package com.dfire.monitor.service.impl;

import java.util.HashMap;

import javax.mail.MessagingException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import com.dfire.common.service.EmailService;
import com.dfire.logs.ErrorLog;
import com.dfire.logs.MonitorLog;
import com.dfire.monitor.config.SmsConfig;
import com.dfire.monitor.domain.AlarmInfo;
import com.dfire.monitor.domain.CbestAlarmInfo;
import com.dfire.monitor.service.AlarmCenter;


/**
 * desc:
 *
 * @author scx
 * @create 2019/04/27
 */
@Service
public class AlarmCenterImpl implements AlarmCenter {

    @Autowired
    private SmsConfig smsConfig;

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private EmailService emailService;

    @Override
    public boolean sendToWeChat(AlarmInfo alarmInfo) {
        MonitorLog.info("[微信告警]userInfo:{},消息结果:{}", alarmInfo.toString(), "请管理员自己设置企业微信告警配置");
        return sendSms(alarmInfo);
    }

    @Override
    public boolean sendToPhone(AlarmInfo alarmInfo) {
        MonitorLog.info("[电话告警]userInfo:{},消息结果:{}", alarmInfo.toString(), "请管理员自己设置企业微信告警配置");
        if (alarmInfo instanceof CbestAlarmInfo) {
        	return sendCbestSms((CbestAlarmInfo)alarmInfo);
        } else {
        	return sendSms(alarmInfo);
        }
    }

    public boolean sendSms(AlarmInfo alarmInfo){
        String url = smsConfig.getUrl();
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON_UTF8);

        //设置访问参数
        HashMap<String, Object> params = new HashMap<>();
        params.put("phonestr", alarmInfo.getPhone());
        params.put("msg", alarmInfo.getMessage());

        //设置访问的Entity
        HttpEntity<HashMap<String,Object>> entity = new HttpEntity<>(params, headers);


        try {
            //发起一个POST请求
            ResponseEntity<String> result = restTemplate.exchange(url, HttpMethod.POST, entity, String.class);
            MonitorLog.info("告警发送返回：{}",result.getBody());
            return true;
        } catch (Exception e) {

        }
        return false;
    }
    
    public boolean sendCbestSms(CbestAlarmInfo alarmInfo){
    	String url = smsConfig.getUrl();
    	HttpHeaders headers = new HttpHeaders();
    	headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
    	
    	//设置访问的Entity
    	HttpEntity<CbestAlarmInfo> entity = new HttpEntity<>(alarmInfo, headers);
    	
    	
    	try {
    		//发起一个POST请求
    		ResponseEntity<String> result = restTemplate.exchange(url, HttpMethod.POST, entity, String.class);
    		MonitorLog.info("告警发送返回：{}",result.getBody());
    		return true;
    	} catch (Exception e) {
    		
    	}
    	return false;
    }

    @Override
    public boolean sendToEmail(String title, String content, String address) {
        try {
            emailService.sendEmail(title, content, address);
            return true;
        } catch (MessagingException e) {
            ErrorLog.error("发送邮件[title:" + title + ",content:" + content + "]失败", e);

        }
        return false;
    }
}
